<template>
  <div>
    <el-table
      :data="list"
      size="mini"
      height="200"
      stripe
      border
      style="width: 100%"
      @current-change="handleCurrentChange"
      @row-click="handleTableRow"
      highlight-current-row
    >
      <el-table-column
        prop="username"
        label="用户名"
        width="180"
      ></el-table-column>
      <el-table-column prop="pwd" label="密码" width="180"></el-table-column>
      <el-table-column prop="power" label="等级" width="180"></el-table-column>
      <el-table-column
        prop="trueName"
        label="真实姓名"
        width="200"
      ></el-table-column>
    </el-table>

    <div class="block" style="margin-top: 15px">
      <el-pagination
        align="center"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="currentPage"
        :page-sizes="[1, 5, 10, 20]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="pages"
      >
      </el-pagination>
    </div>

    <el-button
      type="primary"
      @click="del(userId)"
      style="margin-top: 100px; margin-left: 100px"
      >删除</el-button
    >
    <div>
      <el-button type="primary" @click="update(userId)" style="margin-top:30px;margin-left:50px"
        >修改用户信息</el-button
      >

      <el-dialog
        title="用户个人信息"
        :visible.sync="dialogVisible"
        width="30%"
        :before-close="handleClose"
      >
        
        <span slot="footer" class="dialog-footer">
          <el-button @click="dialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="dialogVisible = false"
            >确 定</el-button
          >
        </span>
      </el-dialog>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      list: [],
      userParam: {
        pageNO: "1",
        pageSize: "3",
        power: "",
      },
      pages: 0,
      currentPage: 1, // 当前页码
      total: 20, // 总条数
      pageSize: 2, // 每页的数据条数
      userId: 0,
      user: {},
      dialogVisible: false,
    };
  },
  methods: {
    async findByPower() {
      let list = await this.ajax.getSubmit(
        "/api/user/findByPower",
        this.userParam
      );
      this.pages = list.pages;
      this.list = list.records;
      console.log(this.list);
    },
    handleTableRow(row) {
      console.log(row);
      console.log(row.userId);
      this.user = row;
      this.userId = row.userId;
    },
    handleCurrentChange(val) {
      this.currentRow = val;
      console.val;
    },
    //每页条数改变时触发 选择一页显示多少行
    async handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      let userParam = { pageNO: 1, pageSize: val, power: "" };
      let info = await this.ajax.getSubmit("/api/user/findByPower", userParam);
      console.log(info);
      this.list = info.records;
    },
    //当前页改变时触发 跳转其他页
    async handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      let userParam = { pageNO: val, pageSize: 3, power: "" };
      let info = await this.ajax.getSubmit("/api/user/findByPower", userParam);
      console.log(info);
      this.list = info.records;
    },
    async del(userId) {
      if (userId == 0) {
        alert("请选择要删除的用户");
        return;
      } else {
        let userParam = { userId: userId };
        let info = await this.ajax.getSubmit("/api/user/delUser", userParam);
        console.log(info);
        if (info == "ok") {
          alert("删除成功");
          this.findByPower();
        }
      }
    },
    async update(userId) {
      if (userId == 0) {
        alert("请选择要修改的用户");
        return;
      } else {
        this.dialogVisible = true;
      }
    },
    handleClose(done) {
      this.$confirm("确认关闭？")
        .then((_) => {
          done();
        })
        .catch((_) => {});
    },
  },
  created() {
    this.findByPower();
  },
};
</script>

<style></style>
